<template>
  <span>
    <el-dialog title="用户密码修改" :visible.sync="dialogFormVisible" @close='handleClose'>
      <!-- 表单 -->
      <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名：" prop="userName">
          <el-input placeholder="请输入用户名" v-model="form.userName" disabled='disabled'></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="pass">
          <el-input placeholder="请输入新密码" v-model="form.pass" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="checkPass">
          <el-input placeholder="请再次输入密码" v-model="form.checkPass" show-password></el-input>
        </el-form-item>                     
      </el-form>
      <!-- 确认按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </span>  
</template>
<script>
export default {
  name:'Add',  
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form: {
        id:'',
        userName:'',
        pass:'',
        checkPass:'',
      },
      dialogFormVisible: true,
      formLabelWidth: '80px',
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ], 
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }    
    }
  },
  created(){
    const id=this.$route.query.id
    this.form.userName=id[0].username
    this.form.id=id[0].id
  },
  methods:{ 
    postAdimnData(){
      this.axios.post('/api/user/edit',{
        params:{
          password:this.form.pass,
          username:this.form.userName,
          id:this.form.id
        }
      }).then((res)=>{
        // 修改成功
        this.message('修改成功','success')
      }).catch((error)=>{
        this.message('服务器忙，请稍后重试','error')
      })
    },    
    handleClose(){
      this.$router.go(-1)
    },
    handleCancel(){
      this.dialogFormVisible=false
    },
    submitForm(ruleForm){
      this.dialogFormVisible = false
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.postAdimnData()          
        } else {
          this.message('服务器忙，请稍后重试','error')
          return false;
        }
      });
    },
    // 失败提示
    message(msg,type){
      this.$message({
        message:msg,
        type
      })
    }
  }
}
</script>
<style scoped>

</style>